let THREE = require("three")
import { CONFIG } from "./config.js"
import { OBJECTS, UTILS, FLAGS } from "./declarations.js"
import { ASSETS } from "./assets.js"
import { WORLD } from "./world/world.js"
import { GLTFLoader } from "../vendor/threejs/loaders/GLTFLoader"
import { DRACOLoader } from "../vendor/threejs/loaders/DRACOLoader"
import { FontLoader } from "../vendor/threejs/loaders/FontLoader"

import * as $ from "jquery"
import TweenMax from "gsap/TweenMaxBase"

const PRELOADER = {
  init: () => {
    UTILS.loadingManager = new THREE.LoadingManager()
    UTILS.audioloader = new THREE.AudioLoader(UTILS.loadingManager)
    UTILS.GLTFLoader = new GLTFLoader(UTILS.loadingManager)
    UTILS.DRACOLoader = new DRACOLoader(UTILS.loadingManager)

    UTILS.DRACOLoader.setDecoderPath("./libs/draco/")
    UTILS.GLTFLoader.setDRACOLoader(UTILS.DRACOLoader)

    UTILS.textureloader = new THREE.TextureLoader(UTILS.loadingManager)
    UTILS.fontloader = new FontLoader(UTILS.loadingManager)
    // console.log(THREE.FontLoader)

    UTILS.loadingManager.onProgress = (item, loaded, total) => {
      let loadedPerc = Math.floor((loaded / total) * 100)

      if (typeof onProgress === "function") FLAGS.loaded = true
      if (loaded === total) {
        

        window.setTimeout(()=>{
          FLAGS.isLoaded = true
          FLAGS.isReady = true
          FLAGS.isEnabled = true

          WORLD.init()
        },500)

        
      } else {
        // TweenMax.to($('#loader-bar'), 1, { width: loadedPerc + '%' })
        $("#loader-text").html(loadedPerc + "%")
      }
    }
    PRELOADER.start()
  },

  start: () => {
    let meshes = (OBJECTS.meshes = {})
    let models = (OBJECTS.models = {})
    let geos = (OBJECTS.geos = {})
    let textures = (OBJECTS.textures = {})
    let fonts = (OBJECTS.fonts = {})
    let audio = (OBJECTS.audio = {})

    // console.log(">>>"+ASSETS.texture.length)

    if (ASSETS.texture.length || ASSETS.model.length) {
      // load up maps/textures and store

      if (ASSETS.texture.length) {
        ASSETS.texture.forEach((e) => {
          UTILS.textureloader.load(CONFIG.path.texture + e.file, (data) => {
            if (e.encoding) data.encoding = e.encoding
            textures[e.name] = data
          })
        })
      }

        // console.log(ASSETS.audio.length)
        // let i = 0
      if (ASSETS.audio.length) {
        ASSETS.audio.forEach((e) => {
          UTILS.audioloader.load(CONFIG.path.audio + e.file, (data) => {
            let _audio = new THREE.Audio(OBJECTS.audioListener)
            let sound = _audio.setBuffer(data)
            _audio.setLoop(true)
            _audio.setVolume(0)
            _audio.analyser = new THREE.AudioAnalyser(sound,64)
            _audio.type = e.type
            audio[e.name] = _audio
            // console.log(i)
            // i++
            // audio[e.name] = new THREE.AudioListener()
          })
        })
      }

      /*
      if (ASSETS.imgseq.length) {

        ASSETS.imgseq.forEach((e) => {
          // OBJECTS.imgseq = e
          // console.log(e, e.total)

          for (let key in e) {
            if (e[key].seq) {
              OBJECTS.imgseq[e[key].name] = {}

              // let o = OBJECTS.imgseq[e[key].name]

              let seq = e[key].seq
              if (seq.length) {
                for (let a = 0; a < seq.length; a++) {
                  let total = seq[a].total
                  let fileext = seq[a].fileext
                  let name = seq[a].name
                  OBJECTS.imgseq[e[key].name][seq[a].name] = []

                  console.log(total)
                  for (let b = 0; b < total; b++) {
                    // preload ...
                    UTILS.textureloader.load(
                      CONFIG.path.texture +
                        "/" +
                        e[key].name +
                        "/" +
                        name +
                        "/" +
                        b +
                        ".jpg",
                      (data) => {

                        OBJECTS.imgseq[e[key].name][seq[a].name][b] = data

                      }
                    )
                  }
                }
              }
            }
          }

        })
      }
      */

      if (ASSETS.model.length) {
        ASSETS.model.forEach((e) => {
          UTILS.GLTFLoader.load(CONFIG.path.mesh + e.file, (data) => {
            models[e.name] = data.scene
            // meshes["caliber"] = data.scene;
            // meshes["caliber"].animations = data.animations;
          })
        })
      }
    } else {
      WORLD.init()
    }
  },
}

export { PRELOADER }
